//1
package TUI;

import Ctr.ItemCtr;
import java.util.Scanner;
/**
 *
 */
public class UpdateItemUI {

    private ItemCtr itemCtr;
    private InputUI inputUI;
    private int itemID;

    /**
     *
     */
    public UpdateItemUI()
    {
        itemCtr = new ItemCtr();
        inputUI = new InputUI();
    }

    /**
     *
     */
    public void start()
    {
        selectItem();
        if(itemID != 0)
        {
            itemMenu();
        }
    }

    /**
     *
     */
    public void itemMenu()
    {
        try
        {
            boolean exit = false;
            while(!exit)
            {
                int choice =   writeItemMenu();
                switch(choice)
                {
                    case 0:
                    exit = true;
                    break;
                    case 1:
                    updateItemName();
                    break;
                    case 2:
                    updateItemPrice();
                    break;
                    case 3:
                    updateDescription();
                    break;
                    case 4:
                    updateMaxInStock();
                    break;
                    case 5:
                    updateMinInStock();
                    break;
                    case 6:
                    updateItemsInStock();
                    break;
                    case 7:
                    updateBrand();
                    break;
                    case 8:
                    updateLocation();
                    break;
                    case 9:
                    selectItem();
                    break;
                }
            }
        }
        catch(Exception e)
        {
            System.out.println("Error");
            itemMenu();
        }
    }

    private int writeItemMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** Update Item ***");
        System.out.println(" Select ItemID: " + itemCtr.getItem(itemID).getItemID());
        System.out.println(" (1) Update Name");
        System.out.println(" (2) Update Price");
        System.out.println(" (3) Update Description");
        System.out.println(" (4) Update Maximum stock");
        System.out.println(" (5) Update Minimum stock");
        System.out.println(" (6) Update Quantity in Stock");
        System.out.println(" (7) Update Company");
        System.out.println(" (8) Update Location");
        System.out.println(" (9) Choose product");
        System.out.println(" (0) Back");
        System.out.print("\n Write choice: ");
        int choice = keyboard.nextInt();
        return choice;
    }

    private void updateItemName()
    {
        String oldItemName = itemCtr.getItem(itemID).getItemName();
        String itemName = inputUI.inputItemName();
        itemCtr.updateItemName(itemID, itemName);
        System.out.println("Product name has been changed from" + oldItemName + "to" + itemName);
        inputUI.pause();
    }

    private void updateItemPrice()
    {
        double oldItemPrice = itemCtr.getItem(itemID).getItemPrice();
        double itemPrice = inputUI.inputItemPrice();
        itemCtr.updateItemPrice(itemID, itemPrice);
        System.out.println("The price has been changed from" + oldItemPrice + "to" + itemPrice);
        inputUI.pause();
    }

    private void updateDescription()
    {
        String oldDescription = itemCtr.getItem(itemID).getDescription();
        String description = inputUI.inputDescription();
        itemCtr.updateDescription(itemID, description);
        System.out.println("The description has been changed from" + oldDescription + "to" + description);
        inputUI.pause();
    }

    private void updateMaxInStock()
    {
        int oldMaxInStock = itemCtr.getItem(itemID).getMaxInStock();
        int maxInStock = inputUI.inputMaxInStock();
        int minInStock = itemCtr.getItem(itemID).getMinInStock();
        int itemsInStock = itemCtr.getItem(itemID).getItemsInStock();
        if(maxInStock < minInStock)
        {
            System.out.println("Maximum stock must be greater than the minimum stock");
            inputUI.pause();
        }
        else
        {
            if(maxInStock >= minInStock && maxInStock >= itemsInStock)
            {
                itemCtr.updateMaxInStock(itemID, maxInStock);
                System.out.println("Maximum stock has been changed from "+ oldMaxInStock +" to " + maxInStock);
                inputUI.pause();
            }
            else
            {
                if(maxInStock < itemsInStock)
                {
                    itemCtr.updateMaxInStock(itemID, maxInStock);
                    System.out.println("Maximum stock has been changed from "+ oldMaxInStock +" to " + maxInStock);
                    System.out.println("Note that the number of items is equal to" + itemsInStock);
                    inputUI.pause();
                }
                else
                {
                    System.out.println("Error:Maximum stock must be greater than the minimum stock");
                    inputUI.pause();
                }
            }
        }
    }

    private void updateMinInStock()
    {
        int oldMinInStock = itemCtr.getItem(itemID).getMinInStock();
        int minInStock = inputUI.inputMinInStock();
        int maxInStock = itemCtr.getItem(itemID).getMaxInStock();
        int itemsInStock = itemCtr.getItem(itemID).getItemsInStock();

        if(minInStock > maxInStock)
        {
            System.out.println("Error:The minimum stock can't be greater than the maximum stock");
            inputUI.pause();
        }
        else
        {
            if(minInStock <= maxInStock && minInStock <= itemsInStock)
            {
                itemCtr.updateMinInStock(itemID, minInStock);
                System.out.println("Minimum stock has been changed from "+ oldMinInStock +" to " + minInStock);
                inputUI.pause();
            }
            else
            {
                if(minInStock > itemsInStock)
                {
                    itemCtr.updateMinInStock(itemID, minInStock);
                    System.out.println("Minimum stock has been changed from "+ oldMinInStock +" to "+ minInStock);
                    System.out.println("Note that the number of items is equal to" + itemsInStock);
                    inputUI.pause();
                }
                else
                {
                    System.out.println("Minimum stock is greater than the maximum stock");
                    inputUI.pause();
                }
            }
        }

    }

    private void updateItemsInStock()
    {
        int oldItemsInStock = itemCtr.getItem(itemID).getItemsInStock();
        int quantity = inputUI.inputUpdateItemQuantity();
        int newItemsInStock = quantity + oldItemsInStock;
        int maxInStock = itemCtr.getItem(itemID).getMaxInStock();
        
        if(newItemsInStock > maxInStock)
        {
            System.out.println("The number of items must not exceed maximum stock, which is " + maxInStock);
            inputUI.pause();
        }
        else
        {
            itemCtr.updateItemsInStock(itemID, newItemsInStock, quantity);
            System.out.println("The number of items has been changed from" + oldItemsInStock + "to" + newItemsInStock);
            inputUI.pause();
        }
    }

    private void updateBrand()
    {
        String oldBrand = itemCtr.getItem(itemID).getBrand();
        String brand = inputUI.inputBrand();
        itemCtr.updateBrand(itemID, brand);
        System.out.println("The Company has been changed from" + oldBrand + "to" + brand);
        inputUI.pause();
    }

    private void updateLocation()
    {
        String oldPlace = itemCtr.getItem(itemID).getPlace();
        String place = inputUI.inputPlace();
        itemCtr.updatePlace(itemID, place);
        System.out.println("The Location has been changed from" + oldPlace + "to" + place);
        inputUI.pause();
    }

    private void selectItem()
    {
        itemID = inputUI.inputID();
        if(itemCtr.getItem(itemID) == null)
        {
            System.out.println("ID not found");
            itemID = 0;
            inputUI.pause();
        }
    }
}
